﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace Support
{
    public static class Aes
    {
        private const string InitialVector = "photobackupws128";
        private const int PasswordIterations = 1;
        private const string HashAlgorithm = "SHA1";
        private const int KeySize = 128;
        private static byte[] InitialVectorBytes = System.Text.Encoding.ASCII.GetBytes(InitialVector);

      


        public static byte[] Encrypt(byte[] dataBytes, byte[] KeyBytes)
        {
            RijndaelManaged SymmetricKey = new RijndaelManaged();
            SymmetricKey.Mode = CipherMode.CBC;
            ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes);
            MemoryStream MemStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write);
            cryptoStream.Write(dataBytes, 0, dataBytes.Length);
            cryptoStream.FlushFinalBlock();
            byte[] CipherTextBytes = MemStream.ToArray();
            MemStream.Close();
            cryptoStream.Close();
            return CipherTextBytes;
        }

        public static byte[] createKeyBytes(string Password, string Salt)
        {
            byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt);
            PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
            byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
            return KeyBytes;
        }


        public static string EncryptString(string PlainText, byte[] KeyBytes)
        {
            byte[] PlainTextBytes = Encoding.UTF8.GetBytes(PlainText);
            byte[] CipherTextBytes = Encrypt(PlainTextBytes, KeyBytes);
            string CipherText = Convert.ToBase64String(CipherTextBytes);
            return CipherText;
        }

        public static byte[] Decrypt(byte[] dataBytes, byte[] KeyBytes)
        {
            RijndaelManaged SymmetricKey = new RijndaelManaged();
            SymmetricKey.Mode = CipherMode.CBC;
            ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes);
            MemoryStream MemStream = new MemoryStream(dataBytes);
            CryptoStream cryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read);
            byte[] PlainTextBytes = new byte[dataBytes.Length];
            int ByteCount = cryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length);
            MemStream.Close();
            cryptoStream.Close();
            return PlainTextBytes;
        }


        public static string DecryptString(string CipherText, byte[] KeyBytes)
        {
            byte[] CipherTextBytes = Convert.FromBase64String(CipherText);
            RijndaelManaged SymmetricKey = new RijndaelManaged();
            SymmetricKey.Mode = CipherMode.CBC;
            ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes);
            MemoryStream MemStream = new MemoryStream(CipherTextBytes);
            CryptoStream cryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read);
            byte[] PlainTextBytes = new byte[CipherTextBytes.Length];
            int ByteCount = cryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length);
            MemStream.Close();
            cryptoStream.Close();
            System.Text.Encoding enc = System.Text.Encoding.UTF8;
            string PlaintText = enc.GetString(PlainTextBytes, 0, ByteCount);
            return PlaintText;
        }

    }
}
